//
//  HomeMidChooseView.m
//  YunBroker
//
//  Created by uplooking on 2017/1/19.
//  Copyright © 2017年 RenLong. All rights reserved.
//

#import "FindMidChooseView.h"

@interface FindMidChooseView()
{

    UIView *vLine;
    UIView *vTopDiv;
    NSMutableArray *arrayTopItem;
    NSInteger flagTopItemSelected;
    
}
@end

@implementation FindMidChooseView


-(instancetype)initWithFrame:(CGRect)frame{
    
    self=[super initWithFrame:frame];
    
    if (self) {
        
        arrayTopItem=[NSMutableArray arrayWithCapacity:0];
    }
    
    
    return self;
    
}


- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    
    if (newSuperview == nil)
    {
        return;
    }
    
    //view 开始加载在superview时的处理方法
    [arrayTopItem removeAllObjects];
    [self initView];
    
}

-(void)reload
{
    for (UIView *view in self.subviews)
    {
        [view removeFromSuperview];
    }
    
    [arrayTopItem removeAllObjects];
    
    
    [self initView];
}



-(void)initView{
    
  
    CGFloat widthLeftItem = 100 * kAutoScanleX;
    
    UIScrollView *scrollTopView = [UIScrollView new];
    [scrollTopView setShowsHorizontalScrollIndicator:NO];
    [scrollTopView setShowsVerticalScrollIndicator:NO];
    [self addSubview:scrollTopView];
    [scrollTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    NSInteger countTop = [_arrayTopTitle count];
    CGFloat  viewWidht= self.frame.size.width;
    CGFloat widthItem = (viewWidht - widthLeftItem)/countTop;
    widthItem = MAX(widthItem, 50 * kAutoScanleX);
    
    if (_arrayTopTitle.count==0) return;
    
    for (NSInteger i = 0 ; i < countTop ; i ++)
    {
        UILabel *label = [[UILabel alloc] init];
        [label setUserInteractionEnabled:YES];
        [label setTag:i];
        [label setTextAlignment:NSTextAlignmentCenter];
        [scrollTopView addSubview:label];
        
        [label setText:_arrayTopTitle[i]];
        [label setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(scrollTopView).offset(i*viewWidht/countTop);
            make.size.mas_equalTo(CGSizeMake(viewWidht/countTop, 45* kAutoScanleX));
        }];
        
      
        
        if (flagTopItemSelected == i)
        {
            [label setTextColor:_textSelectedColor];
        }else
        {
            [label setTextColor:_textNormalColor];
        }
        
        [arrayTopItem addObject:label];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnItemTopPressed:) forControlEvents:UIControlEventTouchUpInside];
        [label addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(label);
        }];
        
        
    }
    
    [scrollTopView setContentSize:CGSizeMake(widthItem * countTop, 45* kAutoScanleX)];
    UILabel *lbSelected = arrayTopItem[flagTopItemSelected];
    vTopDiv = [[UIView alloc] init];
    [vTopDiv setBackgroundColor:_hkColor];
    [scrollTopView addSubview:vTopDiv];
    [vTopDiv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.centerX.equalTo(lbSelected);;
        make.size.mas_equalTo(CGSizeMake(_hkWidth * kAutoScanleX, 2));
    }];
    
    
}

- (void)btnItemTopPressed:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    [self divMoveinitWith:tag];
    
}

- (void)divMoveinitWith:(NSInteger)tag{
    
    
    if (flagTopItemSelected == tag)
    {
        return;
    }
    
    
    flagTopItemSelected = tag;
    
    NSInteger count = [arrayTopItem count];
    
    for (NSInteger i = 0 ; i < count ; i ++)
    {
        UILabel *label = arrayTopItem[i];
        
        if (flagTopItemSelected == i)
        {
            [label setTextColor:_textSelectedColor];
        }else
        {
            [label setTextColor:_textNormalColor];
        }
    }
    
    
    UILabel *lbSelected = arrayTopItem[flagTopItemSelected];
    CGRect frame = vTopDiv.frame;
    frame.origin.x = lbSelected.frame.origin.x + (lbSelected.frame.size.width - frame.size.width) * 0.5;
    
    [UIView animateWithDuration:0.15 animations:^{
        vTopDiv.frame = frame;
    }];
    
    
    if (self.clickAction) {
        self.clickAction(tag);
    }
    

}

@end
